#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 510;
int map[N][N];
int dis[N];
bool st[N];
int n, m;
int dijkstra()
{
	memset(dis, 0x3f, sizeof(dis));
	dis[1] = 0;
	for (int i = 0; i < n; i++) {
		int t = -1;
		for (int j = 1; j <= n; j++) {
			if (!st[j] && (t == -1 || dis[t] > dis[j])) {
				t=j;
			}
		}
		st[t] = 1;
		for (int j = 1; j <= n; j++) {
			dis[j] = min(dis[j], dis[t] + map[t][j]);
		}
	}
	if (dis[n] == 0x3f3f3f3f)return -1;
	return dis[n];
}
int main()
{
	cin >> n >> m;
	memset(map, 0x3f, sizeof(map));
	while (m--) {
		int a, b, u;
		cin >> a >> b >> u;
		map[a][b] = min(map[a][b], u);
	}
	int ret = dijkstra();
	cout << ret << endl;
	return 0;
}